package com.jxs.android.sxyw;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.EomsInfo;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.util.StartHelper;

public abstract class EomsAbstractDetail extends AbstractActivity {

	public static final int GET_DETAIL = 0x001;
	protected final int REQ_SEND_NEXT = 0x002;
	protected final int REQ_DIRECT_PROCESS = 0x003;

	AlertDialog alertDialog = null;

	protected void signAlert(final EomsInfo eomsInfo) {
		final CharSequence[] items = { "不是本地工单驳回", "派发给代维人员处理", "直接处理" };
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				if (item == 0) {
					processReject(eomsInfo);
				} else if (item == 1) {
					sendNext(eomsInfo);
				} else if (item == 2) {
					process(eomsInfo);
				}
				alertDialog.dismiss();
			}
		});
		alertDialog = builder.create();
		alertDialog.show();
	}

	/**
	 * @Des 不是本地工单驳回
	 * @param eomsInfo
	 */
	private void processReject(final EomsInfo eomsInfo) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				String wsdl_url = SelfApplication.WSDL_URL;// wsdl
				String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "processWorkOrderToReject";// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", SelfApplication.getUserInfo().getTicket());
				request.addProperty("case_no", eomsInfo.getCase_no());
				request.addProperty("workOrderType", eomsInfo.getOrderType().index + "");
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						paserReject(envelope.getResponse().toString());
					}
				});

			}
		}).start();
	}

	protected void paserReject(String status) {
		Message msg = new Message();
		msg.what = REJECT_MSG;
		msg.obj = status;
		handler.sendMessage(msg);
	}

	private static final int REJECT_MSG = 0x1013;
	Handler handler = new Handler() {

		/**
		 * 0:成功; -1:无效的ticket; -2:服务端异常; -3:输入参数无效
		 */
		@Override
		public void handleMessage(Message msg) {

			if (msg.what == REJECT_MSG) {
				String status = msg.obj.toString();
				if ("0".equals(status)) {
					resultOk();
					finish();
				} else if ("-1".equals(status)) {
					PopMsg.show("登录已过期,请重新登录");
					StartHelper.startActivity(Login.class);
					finish();
				} else if ("-2".equals(status)) {
					PopMsg.show("服务端异常");
				} else if ("-3".equals(status)) {
					PopMsg.show("输入参数无效");
				}
			}
		}

	};

	protected abstract void resultOk();

	/**
	 * @Des 派发给代维人员处理
	 * @param eomsInfo
	 */
	protected abstract void sendNext(EomsInfo eomsInfo);

	/**
	 * @Des 直接处理
	 * @param eomsInfo
	 */
	protected abstract void process(EomsInfo eomsInfo);

}
